import {
	defineStore
} from 'pinia'

import Utils from '@/utils/common.js'

export const PinaCartStore = defineStore('cart', {
	state: () => ({
		cartList: [],
		maxNum: 99,
		minNum: 1
	}),

	getters: {
		// 是否全选
		Checked: (state) => {
			return state.cartList.length > 0 && state.cartList.every(item => item.checked)
		},
		// 计算总价(选中的商品)
		TotalPrice: (state) => {
			return state.cartList
				.filter(item => item.checked)
				.reduce((total, item) => total + item.salePrice * item.count, 0)
		},
		// 选中的商品数量
		CheckedCount: (state) => {
			return state.cartList.filter(item => item.checked).length
		},
		// 添加新的 getters
		Empty: (state) => state.cartList.length === 0,

		// 计算原价总和
		originalTotal: (state) => {
			return state.cartList
				.filter(item => item.checked)
				.reduce((total, item) => total + item.price * item.count, 0)
		},
		// 添加购物车总数量的 getter
		TotalCount: (state) => {
			return state.cartList.reduce((total, item) => total + item.count, 0)
		},
		// 添加选中商品的总件数（区别于种类数量）
		checkedTotalCount: (state) => {
			return state.cartList
				.filter(item => item.checked)
				.reduce((total, item) => total + item.count, 0)
		}
	},

	actions: {
		// 切换全选状态
		ToggleChecked(checked) {
			this.cartList.forEach(item => item.checked = checked)
		},
		// 切换单个商品选中状态
		ToggleItemChecked(id) {
			const item = this.cartList.find(item => item.id === id)
			if (item) {
				item.checked = !item.checked
			}
		},
		// 修改商品数量
		updateItemNum(id, count) {
			const item = this.cartList.find(item => item.id === id)
			if (item) {
				if (count < this.minNum) {
					uni.showModal({
						title: '提示',
						content: '是否要移除该商品？',
						success: (res) => {
							if (res.confirm) {
								this.removeGoods(id)
							} else {
								item.count = this.minNum
							}
						}
					})
					return
				}
				if (count > item.stock) {
					uni.showToast({
						title: `库存仅剩${item.stock}件`,
						icon: 'none'
					})
					item.count = item.stock
					return
				}
				item.count = count
			}
		},
		// 移除商品
		removeGoods(id) {
			Utils.showConfirm('确定要移除该商品吗？').then((resp) => {
				console.log(resp)
				if (resp) {
					const index = this.cartList.findIndex(item => item.id === id)
					if (index > -1) {
						this.cartList.splice(index, 1)
						Utils.toast('删除成功')
					}
				}
			})
		},
		// 添加商品
		AddToCart(goods) {
			// 判断商品是否已存在
			const _goods = this.cartList.find(item => item.id === goods.id)
			if (_goods) {
				_goods.count++
				Utils.toast('已添加到购物车')
				return
			}
			this.cartList.push({
				...goods,
				count: 1,
				checked: false
			})
			Utils.toast('添加成功')
		},
		// 批量删除选中商品
		removeCheckedItems() {
			if (this.checkedCount === 0) {
				Utils.toast('请先选择商品')
				return
			}
			uni.showModal({
				title: '提示',
				content: '确定要删除选中的商品吗？',
				success: (res) => {
					if (res.confirm) {
						this.cartList = this.cartList.filter(item => !item.checked)
						uni.showToast({
							title: '删除成功',
							icon: 'success'
						})
					}
				}
			})
		}
	}
})